
# Include the BZFlag ruby module
include Bzflag

# Test class

class Test < Plugin

	def initialize(args = nil)
		@args = args	
	end

	def unknown_slash_command(event_data)

		cmd = event_data.message.downcase.split[0]
		parm = event_data.message.split[1..-1]

		if cmd == "/bz_get_group_list"
			bz_get_group_list_meth(event_data.from)
			event_data.handled = true
		end

		if cmd == "/bz_is_player_paused"
			bz_is_player_paused_meth(event_data.from, parm[0].to_i)
			event_data.handled = true
		end

		if cmd == "/bz_get_player_flag"
			bz_get_player_flag_meth(event_data.from, parm[0].to_i)
			event_data.handled = true
		end

		if cmd == "/bz_set_player_operator"
			bz_set_player_operator_meth(event_data.from, parm[0].to_i)
			event_data.handled = true
		end

		if cmd == "/bz_get_group_perms"
			bz_get_group_perms_meth(event_data.from, parm[0].to_s)
			event_data.handled = true
		end

		if cmd == "/bz_group_allow_perm"
			bz_group_allow_perm_meth(event_data.from, parm)
			event_data.handled = true
		end

		if cmd == "/bz_get_debug_level"
			bz_get_debug_level_meth(event_data.from)
			event_data.handled = true
		end

		if cmd == "/bz_kick_user"
			bz_kick_user_meth(event_data.from, parm)
			event_data.handled = true
		end

		if cmd == "/bz_get_reports"
			bz_get_reports_meth(event_data.from)
			event_data.handled = true
		end

	end

	##  commands ##
	
	def bz_get_group_list_meth(to)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_get_group_list_meth")
		bz_get_group_list.each { |group| bz_send_text_message(BZ_SERVER, to, "#{group}") }
		bz_send_text_message(BZ_SERVER, to, "end => bz_get_group_list_meth")
	end


	def bz_is_player_paused_meth(to, player_id)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_is_player_paused_meth")

		player = Player.new(player_id)

		if bz_is_player_paused?(player_id)
			bz_send_text_message(BZ_SERVER, to, "player #{player.callsign} is paused")
		else
			bz_send_text_message(BZ_SERVER, to, "player #{player.callsign} is NOT paused")
		end

		bz_send_text_message(BZ_SERVER, to, "end => bz_is_player_paused_meth")
	end

	def bz_get_player_flag_meth(to, player_id)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_get_player_flag_meth")

		player = Player.new(player_id)
		flag = bz_get_player_flag(player_id)
	
		if flag
			bz_send_text_message(BZ_SERVER, to, "player #{player.callsign} has #{flag} flag")
		else
			bz_send_text_message(BZ_SERVER, to, "player #{player.callsign} has NO flag")
		end

		bz_send_text_message(BZ_SERVER, to, "end => bz_get_player_flag_meth")
	end


	def bz_set_player_operator_meth(to, player_id)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_set_player_operator_meth")

		player = Player.new(player_id)

		if bz_set_player_operator(player_id)
			bz_send_text_message(BZ_SERVER, to, "Set operator on #{player.callsign} is successfull")
		else
			bz_send_text_message(BZ_SERVER, to, "Set operator on #{player.callsign} failed")
		end
		
		bz_send_text_message(BZ_SERVER, to, "end => bz_set_player_operator_meth")

	end

	def bz_get_group_perms_meth(to, group)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_get_group_perms_meth")
		bz_get_group_perms(group).each { |perm| bz_send_text_message(BZ_SERVER, to, "* #{perm}") }
		bz_send_text_message(BZ_SERVER, to, "end => bz_get_group_perms_meth")
	end

	def bz_group_allow_perm_meth(to, parm)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_group_allow_perm_meth")

		group = parm[0]
		perm = parm[1]

		puts "DEBUG :: parm =>  #{parm} - perm => #{perm} - group => #{group}"

		if bz_group_allow_perms(group, perm)
			bz_send_text_message(BZ_SERVER, to, "Adding #{perm} to group #{group} succeeded")
		else
			bz_send_text_message(BZ_SERVER, to, "Adding #{perm} to group #{group} failed")
		end

		bz_send_text_message(BZ_SERVER, to, "end => bz_group_allow_perm_meth")

	end

	def bz_get_debug_level_meth(to)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_get_debug_level_meth")
		bz_send_text_message(BZ_SERVER, to, "debuglevel is #{bz_get_debug_level}")
		bz_send_text_message(BZ_SERVER, to, "end => bz_get_debug_level_meth")
	end

	def bz_kick_user_meth(to, parm)

		bz_send_text_message(BZ_SERVER, to, "begin => bz_kick_user_meth")


		player_id = parm[0].to_i
		reason = parm[1..-1].join(" ")

		puts "DEBUG :: bz_kick_user_meth :: player_id =>  #{player_id} - reason => #{reason}"

		player = Player.new(player_id)

		if bz_kick_user(player_id, reason, false)
			bz_send_text_message(BZ_SERVER, to, "Player #{player.callsign} got kicked for #{reason}")
		else
			bz_send_text_message(BZ_SERVER, to, "Kick of Player #{player.callsign} failed")
		end

		bz_send_text_message(BZ_SERVER, to, "end => bz_kick_user_meth")

	end

	def bz_get_reports_meth(to)
		bz_send_text_message(BZ_SERVER, to, "begin => bz_get_reports_meth")
		bz_get_reports.each { |report| bz_send_text_message(BZ_SERVER, to, "* #{report}") }
		bz_send_text_message(BZ_SERVER, to, "end => bz_get_reports_meth")
	end

end

# create new Test plugin instance
t = Test.new

# register the plugin instance
t.register
